package com.baizhi.cmfz.controller;

import com.baizhi.cmfz.annotation.LogAnnotation;
import com.baizhi.cmfz.entiy.Admin;
import com.baizhi.cmfz.service.AdminService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping("admin")
public class AdminController {

    private Logger logger= LoggerFactory.getLogger(AdminController.class);

    @Autowired
    private AdminService service;

    @RequestMapping("login")
    @LogAnnotation("登录")
    public String login(String username, String password) {

        logger.debug("username"+username+"password"+password);
        //页面输入的账号密码封装到token中
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);

        Session session = null;
        try {
            //直接调用Subject的登录方法
            Subject subject = SecurityUtils.getSubject();
            subject.login(usernamePasswordToken);

            //获取登录成功的用户的名字
            Object principal = subject.getPrincipal();
            //判断当前用户有没有登录成功
            boolean authenticated = subject.isAuthenticated();
            //获取Shiro的Session  和HttpSession 是互相独立的  空间不共享
            Admin admin = new Admin();
            admin.setUsername(username);
            admin.setPassword(password);
            subject.getSession().setAttribute("admin", admin);
            session = subject.getSession();

        } catch (UnknownAccountException e) {
            return "redirect:/login.jsp";
        }catch (IncorrectCredentialsException e){
            return "redirect:/login.jsp";
        }
        return "main/main";


        /*Admin admin = service.selectOne(username, password);
        //System.out.println(admin);
        if (admin != null) {
            session.setAttribute("login", admin);
            return "main/main";
        } else {
            return "login";

        }*/
    }
    @RequestMapping("updatePassword")
    @ResponseBody
    @LogAnnotation("修改管理员密码")
    public Map updatePassword(Admin admin, HttpSession session){

        HashMap map = new HashMap();
        try {
            service.update(admin);
            session.removeAttribute("login");
            map.put("isUpdate",true);
            System.out.println("修改");
        } catch (Exception e) {
            map.put("isUpdate",false);
            e.printStackTrace();
        }
        //System.out.println(admin+"修改密码");
        return map;
    }
    @LogAnnotation("管理员注销")
    @RequestMapping("loginOut")
    public String loginOut(HttpSession session){
        System.out.println("注销");
        session.removeAttribute("login");

        return "login";
    }
}
